<template>
  <view class="instrument">
    <scroll-view @scrolltolower="scrolltolower" class="scrollView" refresher-background="rgba(0, 0, 0, 0)" refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" @refresherrefresh="onRefresh" @refresherrestore="onRestore" :style="{'height':  'calc('+ screenHeight  +'px' +' ' + '-' +' ' + excessHeight +'rpx)'}" scroll-y="true">
      <view class="box" v-for="(item,index) in list" :key="index" @click.stop="toDetail(item)">
        <view class="demo-avatar" v-if="item.is_adv!=1">
          <view class="pic">
            <image class="point-tag" @click.stop="toUserDetail(item)" :src="item.avatar"></image>
          </view>
          <view class="name" @click.stop="toUserDetail(item)"> {{item.nickname}}</view>
        </view>
        <u-gap height="1" v-if="item.is_adv==1"></u-gap>

        <view class="demo-tag-address">

          <view class="demo-tag">
            <view class="pic">
              <image class="img-tag" src="/static/images/indexpage/topic-icon.png"></image>
            </view>
            <view class="price"> {{item.is_adv!=1?item.label:'广告'}}</view>
          </view>
        </view>
        <view class="demo-topic">
          {{item.title}}
        </view>
        <view class="demo-title">
          {{item.main_content}}
        </view>
        <view class="img-st">
          <image class="img-tag" :src="item.main_image"></image>
        </view>
        <view class="bottom-box" v-if="item.is_adv!=1">
          <view class="img">
            <image class="img-tag" :src="item.good_status==0?'/static/images/indexpage/zan.png':'/static/images/indexpage/tozan.png'" @click.stop="getTrendsGoods(item)"></image>
          </view>
          <view class="value">{{item.good_num}}</view>
          <view class="img">
            <image class="img-tag" src="/static/images/indexpage/kan.png"></image>
          </view>
          <view class="value">{{item.see_num}}</view>
          <view class="img">
            <image class="img-tag" src="/static/images/indexpage/yan.png"></image>
          </view>
          <view class="value">{{item.comment_num}}</view>
        </view>
      </view>
      <view class="nodata-st" v-if="form.page==form.last_page"> ------没有更多数据了------</view>
      <view class="nodata-st" v-if="list.length ==0"> ------暂无数据------</view>
      <view style="height:24rpx;"></view>
    </scroll-view>

  </view>
</template>

<script>
import api from '@/api/index.js';
export default {
  name: "x-instrument",
  props: {
    screenHeight: {
      type: Number,
      default: uni.getStorageSync('screenHeight') - uni.getStorageSync('safeAreaBottom')
    },
    excessHeight: {
      type: Number,
      default: 0
    },
    query: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      triggered: true,
      list: [],
      form: {
        page: 1,
        page_size: 10,
        last_page: 0,
      },
    };
  },

  mounted() {

    // this.init()
    console.log(this.query)
    this._freshing = false;
    setTimeout(() => {
      this.triggered = true;
    }, 1000)
  },

  methods: {
    init() {
      this.form.page = 1;
      this.form.last_page = 0;
      this.getpage()
    },
    getpage() {
      api.base.getTrends({ ...this.form, ...this.query }).then(res => {
        if (res.code == 1) {
          this.list = this.form.page === 1 ? res.data.data : this.list.concat(res.data.data);
          this.form.last_page = res.data.last_page;
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      });
    },
    onRefresh() {
      if (this._freshing) return;
      this._freshing = true;
      // this.init()
      this.$emit('initPage');
      setTimeout(() => {
        this.triggered = false;
        this._freshing = false;
      }, 1000)
    },
    onRestore() {
      this.triggered = 'restore'; // 需要重置
      console.log("onRestore");
    },
    scrolltolower() {
      if (this.form.page < this.form.last_page) {
        this.form.page += 1;
        this.getpage();
      }
    },
    //点赞取消
    getTrendsGoods(item) {
      api.base.getTrendsGoods({ ...{ id: item.id }, ...this.query }).then(res => {
        if (res.code == 1) {
          uni.$u.toast(res.msg)
          if (item.good_status == 0) {
            item.good_status = 1
            item.good_num++
          } else {
            item.good_status = 0
            item.good_num--
          }
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      });
    },
    toDetail(item) {
      //  跳转广告 或详情
      if (item.is_adv == 1) {
        uni.navigateTo({
          url: `/pages/componets/instrument/advertisement/index?id=` + item.id
        });
      } else {
        uni.navigateTo({
          url: `/pages/componets/currentCircle/instrumentDetail/index?types=` + this.query.types + '&id=' + item.id
        });
      }
    },
    toUserDetail(item) {
      if (uni.getStorageSync('userinfo').user_id == item.user_id) {
        uni.switchTab({
          url: '/pages/my/my'
        })
      } else {
        uni.navigateTo({
          url: `/pages/otherPeople/index?user_id=` + item.user_id
        });
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.instrument {
  .box {
    margin: 0rpx 32rpx 32rpx 32rpx;
    width: calc(100% - 64rpx);
    // height: 753rpx;
    background: #ffffff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    padding-bottom: 24rpx;
    .demo-avatar {
      height: 86rpx;
      line-height: 66rpx;
      padding-top: 24rpx;
      padding-left: 16rpx;
      margin-bottom: 16rpx;
      display: flex;
      .pic {
        display: flex;
        justify-content: center;
        align-items: center;
        float: left;
        width: 66rpx;
        height: 66rpx;
        .point-tag {
          width: 66rpx;
          height: 66rpx;
          border-radius: 7694rpx 7694rpx 7694rpx 7694rpx;
        }
      }
      .name {
        height: 66rpx;
        line-height: 66rpx;
        padding-left: 16rpx;
        padding-right: 16rpx;
        font-size: 32rpx;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #333333;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
      }
    }
    .demo-tag-address {
      margin: 26rpx 16rpx 16rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 47rpx;
      display: flex;
      // justify-content: space-between;
      .demo-tag {
        padding-left: 16rpx;
        padding-right: 16rpx;
        display: flex;
        // width: 168rpx;
        height: 47rpx;
        background: rgba(109, 12, 247, 0.1);
        border-radius: 44rpx 44rpx 44rpx 44rpx;
        justify-content: center;
        align-items: center;
        .pic {
          width: 33rpx;
          height: 38rpx;
          // border-radius: 100rpx;
          // background: #6d0cf7;
          font-size: 24rpx;
          color: #ffffff;
          display: flex;
          justify-content: center;
          align-content: center;
          .img-tag {
            width: 33rpx;
            height: 33rpx;
          }
        }
        .price {
          padding-left: 14rpx;
          font-size: 24rpx;
          color: #6d0cf7;
          height: 47rpx;
          line-height: 47rpx;
        }
      }
      .demo-address {
        padding-left: 16rpx;
        padding-right: 16rpx;
        background: #f3f3f3;
        .price {
          color: #333333;
        }
        .pic {
          height: 47rpx;
          line-height: 47rpx;
          width: 24rpx;
          height: 24rpx;
          // border-radius: 100rpx;
          // background: #6d0cf7;
          font-size: 24rpx;
          color: #ffffff;
          display: flex;
          justify-content: center;
          align-content: center;
          .img-tag {
            width: 24rpx;
            height: 24rpx;
          }
        }
      }
    }
    .demo-topic {
      margin: 15rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      font-size: 28rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: bold;
      color: #333333;
      // height: 36rpx;
      line-height: 36rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
    .demo-title {
      margin: 12rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      font-size: 24rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      color: #666666;
      // height: 72rpx;
      line-height: 36rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
    .img-st {
      margin: 8rpx 16rpx 4rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 360rpx;
      .img-tag {
        width: 100%;
        height: 360rpx;
        border-radius: 12rpx 12rpx 12rpx 12rpx;
      }
    }
    .bottom-box {
      margin: 35rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 40rpx;
      line-height: 40rpx;
      display: flex;
      .img {
        width: 40rpx;
        margin-right: 10rpx;
        .img-tag {
          width: 40rpx;
          height: 40rpx;
        }
      }
      .value {
        font-size: 24rpx;
        font-family: HarmonyOS Sans-Regular, HarmonyOS Sans;
        font-weight: 400;
        color: #333333;
        padding-right: 30rpx;
      }
    }
  }
}
</style>